//------------------------------------------------------------------------------
//  FileSystem.cpp
//  (C) 2009 xoyojank
//------------------------------------------------------------------------------
#include "StdEddy.h"
#include "Common/FileSystem.h"


//------------------------------------------------------------------------------
namespace Eddy
{

//------------------------------------------------------------------------------
bool
FileSystem::DeleteFile(const WString& path)
{
    assert(!path.empty());

    WString fullPath;
    ToFullPath(fullPath, path);

    return (0 != ::DeleteFile(fullPath.c_str()));
}

//------------------------------------------------------------------------------
bool
FileSystem::DeleteDirectory(const WString& path)
{
    assert(!path.empty());

    WString fullPath;
    ToFullPath(fullPath, path);

    return (0 != RemoveDirectory(fullPath.c_str()));
}

//------------------------------------------------------------------------------
bool
FileSystem::IsFileExist(const WString& path)
{
    assert(!path.empty());

    WString fullPath;
    ToFullPath(fullPath, path);

    DWORD fileAttrs = GetFileAttributes(fullPath.c_str());
    return ((INVALID_FILE_ATTRIBUTES != fileAttrs) &&
            (0 == (FILE_ATTRIBUTE_DIRECTORY & fileAttrs)));
}

//------------------------------------------------------------------------------
bool
FileSystem::IsDirectoryExist(const WString& path)
{
    assert(!path.empty());

    WString fullPath;
    ToFullPath(fullPath, path);

    DWORD fileAttrs = GetFileAttributes(fullPath.c_str());
    return ((INVALID_FILE_ATTRIBUTES != fileAttrs) &&
            (0 != (FILE_ATTRIBUTE_DIRECTORY & fileAttrs)));
}

//------------------------------------------------------------------------------
bool
FileSystem::CreateDirectory(const WString& path)
{
    assert(!path.empty());

    WString fullPath;
    ToFullPath(fullPath, path);

    return (0 != ::CreateDirectory(fullPath.c_str(), NULL));
}

//------------------------------------------------------------------------------
std::vector<WString>
FileSystem::ListFiles(const WString& dirPath, const WString& pattern)
{
    assert(!dirPath.empty());
    assert(!pattern.empty());

    WString fullPath;
    ToFullPath(fullPath, dirPath);
    if ('\\' != fullPath[fullPath.length() - 1])
        fullPath.append(L"\\");

    WString path = fullPath + pattern;

    std::vector<WString> result;
    WIN32_FIND_DATA findFileData;
    HANDLE hFind = FindFirstFile(path.c_str(), &findFileData);
    if (INVALID_HANDLE_VALUE != hFind)
    {
        do
        {
            if (0 == (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
                result.push_back(findFileData.cFileName);
        }
        while (FindNextFile(hFind, &findFileData));
        FindClose(hFind);
    }
    return result;
}

//------------------------------------------------------------------------------
std::vector<WString>
FileSystem::ListDirectories(const WString& dirPath, const WString& pattern)
{
    assert(!dirPath.empty());
    assert(!pattern.empty());


    WString fullPath;
    ToFullPath(fullPath, dirPath);
    if ('\\' != fullPath[fullPath.length() - 1])
        fullPath.append(L"\\");

    WString path = fullPath + pattern;

    std::vector<WString> result;
    WIN32_FIND_DATA findFileData;
    HANDLE hFind = FindFirstFile(path.c_str(), &findFileData);
    if (INVALID_HANDLE_VALUE != hFind)
    {
        do
        {
            if ((0 != (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) &&
                    (0 != wcscmp(findFileData.cFileName, L"..")) &&
                    (0 != wcscmp(findFileData.cFileName, L".")))
                result.push_back(findFileData.cFileName);
        }
        while (FindNextFile(hFind, &findFileData));
        FindClose(hFind);
    }
    return result;
}

//------------------------------------------------------------------------------
WString
FileSystem::GetBinDirectory()
{
    TCHAR path[MAX_PATH];
    DWORD res = GetModuleFileName(NULL, path, MAX_PATH);
    assert(0 != res);
    // remove filename
    for (int i = res; i > 0; --i)
    {
        if ('\\' == path[i])
        {
            path[i + 1] = 0;
            break;
        }
    }

    return path;
}

}// Eddy